/*
Script: 	
	JVLazyLoad - Lazy loading.

License:
	Proprietary - JoomlaVi Club members only

Copyright:
	Copyright (C) JoomlaVi. All rights reserved.
*/

var JVLazyLoad = new Class({
    Implements: [Options],
	options: {
		replacer: 'templates/jv-framework/themes/default/images/trans.gif',
		selectors: 'img',
		duration: 750
	},
	
    initialize: function(options){		
		this.setOptions(options);
		this.selectors = $$(this.options.selectors);
		if(!this.selectors.length){
			this.selectors = $$(this.options.selectors);
		}
		this.selectors.each(function(selector){
            selector.osrc = selector.src;
			selector.src = this.options.replacer;
        }.bind(this));
		
		window.addEvent('scroll', this.initLoad.bind(this));
		this.initLoad();
    },
	
	initLoad: function(){
		var that = this;
		this.selectors.each(function(selector){
			if(selector.getCoordinates().top < window.getHeight() + window.getScrollTop()){
				if(!selector.loaded){
					selector.loaded = true;
					
					new Asset.image(selector.osrc, {
						onload: function(){
							selector.src = selector.osrc;
							new Fx.Morph(selector, {duration: that.options.duration}).set({opacity: 0}).start({opacity: 1});
						}
					});
				}
			}
		});	
	}
});

	
/*
Script: 	
	JVEqualHeight - Equal elements height.

License:
	Proprietary - JoomlaVi Club members only

Copyright:
	Copyright (C) JoomlaVi. All rights reserved.
*/

var JVEqualHeight = function(selectors){
	var maxHeight = 0;
	$$(selectors).each(function(selector){
		maxHeight = Math.max(maxHeight, selector.getCoordinates().height);
	});
	$$(selectors).each(function(selector){
		selector.setStyle(window.ie6 ? 'height' : 'min-height', maxHeight);
	});
};


/*
Script: 	
	JVEffects - JV Effects such as background color.

License:
	Proprietary - JoomlaVi Club members only

Copyright:
	Copyright (C) JoomlaVi. All rights reserved.
*/

var JVEffects = new Class({
    Implements: [Options],	
	options:{		
		fxDuration: 350,
		fxTransition: Fx.Transitions.linear,
		wait: false
	},
	
    initialize: function(selectors, fxPropertiesFrom, fxPropertiesTo, options){
        this.setOptions(options);        
        $$(selectors).each(function(selector, index){
            var selectorFx = new Fx.Styles(selector, this.options);            
			selector.addEvents({
				'mouseenter': function(){
					selectorFx.stop().start(fxPropertiesFrom);
				},
				'mouseleave': function(){
					selectorFx.stop().start(fxPropertiesTo);
				}
			});
        });
    }
});



/*
Script: 	
	JVTop - Scroll to top effect.

License:
	Proprietary - JoomlaVi Club members only

Copyright:
	Copyright (C) JoomlaVi. All rights reserved.
*/

var JVTop = new Class({
    Implements: [Options],
	options:{		
		fxDuration: 350,
		fxTransition: Fx.Transitions.linear,
		wait: false
	},
	
    initialize: function(options){
        this.setOptions(options);        
        var topElement = new Element('div', {
			'id': 'toTop',
            'html': '' 
		}).inject(document.body);
        topElement.addEvent('click', function(){
            new Fx.Scroll(window).toTop();
        });
		var topFx = new Fx.Morph(topElement, this.options).set({'opacity': 0});
		window.addEvent('scroll', function(){
			if(window.getScrollTop() != 0){
				topFx.cancel().start({'opacity': 1});
			}
			else{
				topFx.cancel().start({'opacity': 0});
			}
		});
    }
});

/*
Script: 	
	JVUserTools - User Tools.

License:
	Proprietary - JoomlaVi Club members only

Copyright:
	Copyright (C) JoomlaVi. All rights reserved.
*/

var JVUserTools = new Class({

    initialize: function(options){
		var userPanel = $('userpanel');
        var userPanels = $('userpanels');
		if(!userPanel) return;
		var userPanelCoord = userPanel.getCoordinates();
		var isShowed = false, offsetW = 3;
	    var isRTL = $$('body')[0].hasClass('rtl');
        if(isRTL){
            var userPanelFx = new Fx.Morph(userPanels).set({'left': -userPanelCoord.width - offsetW});
        }
        else{
            var userPanelFx = new Fx.Morph(userPanels).set({'right': -userPanelCoord.width - offsetW});        
        }

    	$('toggle').addEvent('click', function(e){
			e = new Event(e).stop();	
			isShowed = !isShowed;
            if(isRTL){
                userPanelFx.start({'left': isShowed ? 0 : -userPanelCoord.width - offsetW});
            }
            else{
            	userPanelFx.start({'right': isShowed ? 0 : -userPanelCoord.width - offsetW});
            }		
		});	        
       
	    var labels = userPanel.getElements('.layouttype label');    
        var label2 = userPanel.getElements('.temp_color label');
            
		labels.each(function(labelTag){
			labelTag.addEvent('click', function(){
				labels.removeClass('active');	
				this.addClass('active');
                this.getPrevious().checked = true;	
			});
		});
       
		label2.each(function(labelTag){
			labelTag.addEvent('click', function(){
				label2.removeClass('active');	
				this.addClass('active');
                this.getPrevious().checked = true;	
			});
		});
		

		var resetBtn = userPanel.getElement('input[type=reset]');
        var applyBtn = userPanel.getElement('input[type=submit]');
        
        if(resetBtn){

          	resetBtn.addEvent('click', function(){
                for(var i = 0; i < userPanel.elements.length; i++)
                {
                    if(userPanel.elements[i].checked && userPanel.elements[i].type == 'radio' || userPanel.elements[i].type != 'radio'){
                        if(userPanel.elements[i].value != 'Apply' && userPanel.elements[i].value != 'Reset'){
                            Cookie.dispose(userPanel.elements[i].name);
                        }
                    }                    
                } 
                 window.location = window.location;    
            });
        }
        



        if(applyBtn){
          	applyBtn.addEvent('click', function(){
                for(var i = 0; i < userPanel.elements.length; i++)
                {
                    if(userPanel.elements[i].checked && userPanel.elements[i].type == 'radio' || userPanel.elements[i].type != 'radio'){
                        if(userPanel.elements[i].value != 'Apply' && userPanel.elements[i].value != 'Reset'){ 
                            Cookie.write(userPanel.elements[i].name,userPanel.elements[i].value);
                        }
                    }
                } 

                 window.location = window.location;    
            });
        }
    },    
    
    changeVersion: function(theme,version){
        Cookie.write(theme+'_version',version);
        window.location = window.location;   
    },
    
    showSearch: function(itemid){
        var search  = $('mobileSearch');
        var display = search.getStyle('display');
        search.setStyle('display', display == 'block' ? 'none' : 'block');    
        
      	
        
    } 
      
});

var JVUserTools;
		
window.addEvent('domready',function(){	
	JVUserTools = new JVUserTools();

});

function gotourl(url){  
  window.location.href= url;   
}  